//
//  NSString+ZXDrawing.m
//  Runner
//
//  Created by simon on 2021/8/4.
//

#import "NSString+ZXDrawing.h"

@implementation NSString (ZXDrawing)

+ (CGSize)zx_boundingSizeOfString:(NSString *)text withSize:(CGSize)size font:(UIFont *)font
{
    if (CGSizeEqualToSize(size, CGSizeZero))
    {
        size = CGSizeMake(MAXFLOAT, MAXFLOAT);
    }
    if (!font) {
        font = [UIFont systemFontOfSize:12];
    }
    NSMutableDictionary *attrMDic = [NSMutableDictionary new];
    [attrMDic setObject:font forKey:NSFontAttributeName];
    CGSize textSize = [text boundingRectWithSize:size
                              options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:attrMDic
                              context:nil].size;
    return textSize;
}


+ (CGSize)zx_boundingSizeOfString:(NSString *)text
                         withSize:(CGSize)size
                         withFont:(UIFont *)font
                  withLineSpacing:(CGFloat)lineSpacing
{
    if (CGSizeEqualToSize(size, CGSizeZero))
    {
        size = CGSizeMake(MAXFLOAT, MAXFLOAT);
    }
    if (!font) {
        font = [UIFont systemFontOfSize:12];
    }
    NSMutableDictionary *attrMDic = [NSMutableDictionary new];
    [attrMDic setObject:font forKey:NSFontAttributeName];
    
    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    style.lineSpacing = lineSpacing;
    //label的默认方式
    style.lineBreakMode = NSLineBreakByTruncatingTail;
    [attrMDic setObject:style forKey:NSParagraphStyleAttributeName];
    
    CGSize textSize = [text boundingRectWithSize:size
                              options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                           attributes:attrMDic
                              context:nil].size;
    return textSize;
}

+ (void)zx_drawTextInContext:(CGContextRef)ctx text:(NSString *)text inRect:(CGRect)rect font:(UIFont *)font
{
    NSMutableParagraphStyle *priceParagraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    priceParagraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    priceParagraphStyle.alignment = NSTextAlignmentLeft;
    
    [text drawInRect:rect
      withAttributes:@{NSParagraphStyleAttributeName : priceParagraphStyle, NSFontAttributeName : font}];
}


+ (NSUInteger)zx_numberLinesOfDefualtParagraphString:(NSString *)text
                                           boundSize:(CGSize)size
                                                font:(UIFont *)font{
    
    if (text.length == 0) {
        return 0;
    }
    NSMutableDictionary *attrMDic = [NSMutableDictionary new];
    [attrMDic setObject:font forKey:NSFontAttributeName];
    
    CGFloat textH = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin| NSStringDrawingUsesFontLeading attributes:attrMDic context:nil].size.height;
    CGFloat lineHeight = font.lineHeight;
    CGFloat a = textH / lineHeight;
    NSUInteger  lineCount = (NSUInteger) a;

    return lineCount;
}



@end
